package com.roast.pages;

import java.util.ArrayList;
import java.util.List;
import com.roast.domain.*;
import com.roast.services.my.MyServicesFacade;
import com.roast.util.EventSSO;
import com.roast.util.ArrayListUtil;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionAttribute;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

public class UserEvent {

    @Inject
    MyServicesFacade myServices;

    @InjectPage
    private UserComments showComments;

    @SessionAttribute
    boolean loginSA;

    @SessionAttribute
    UserData userSA;

    @SessionState
    @Property
    EventSSO eventSSO;

    @Property
    String addressText;

    private boolean addressButton;
    @Property
    String systemMessage = "     ";

    @Property
    Address address;

    public UserData getUserSA() {
        return userSA;
    }

    void onSelectedFromAddressButton() {
        addressButton = true;
    }

    public List<Address> getAddressList() {
        try {
            List<Address> list = eventSSO.getAddressList();
            if (!list.isEmpty()) {
                return list;
            }
        } catch (Exception e) {
            System.out.println("[PAGE:ShowUserEvent:" + e + "]");
        }
        return null;
    }

    public boolean isNoAddressingsTest() {
        return getAddressList() == null;
    }

    public boolean isAddressTest() {
        long userLoginID = userSA.getUserID();
        long masterID = eventSSO.getMasterID();
        long starID = eventSSO.getStarID();

        if (userLoginID == masterID) {
            return false;
        }
        if (userLoginID == starID) {
            return false;
        }

        try {
            if (eventSSO.getAddressList() != null) {
                if (ArrayListUtil.isParticipantAddressStar(userLoginID, eventSSO.getAddressList())) {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            System.out.println("[PAGE:ShowUserEvent:" + e + "]");
        }

        return true;
    }

    Object onSuccess() {
        if (addressButton) {

            Event event = new Event();
            event.setEventID(eventSSO.getEventID());

            Address address = new Address();
            address.setAddressByUser(userSA);

            Text addressTextObj = new Text();
            addressTextObj.setText(addressText);
            address.setAddressText(addressTextObj);

            ArrayList<Address> addressList = new ArrayList<>();
            addressList.add(address);

            event.setAddressList(addressList);

            int signal = myServices.addressToStar(event);
            if (signal == 10700) {
                eventSSO.setEvent(event);
                systemMessage = "You have successfully addressed the star!";
            } else {
                systemMessage = "Speech failed!";
            }
        }
        return null;
    }

    @OnEvent(component = "commentLink")
    Object onShowEvent(long id) {
        Address addressShow = new Address();
        addressShow.setAddressID(id);

        int signal = myServices.getAddress(addressShow);

        if (signal == 10900) {
            showComments.setAddress(addressShow);
            showComments.setStarID(eventSSO.getStarID());
            return showComments;
        }
        systemMessage = "Fail to show comments!";
        return UserEvent.class;
    }

    Object onActivate() {
        if (!loginSA) {
            return Index.class;
        } else {
            return null;
        }
    }

    void onActivate(String systemMessage) {
        this.systemMessage = systemMessage;
    }

    String onPassivate() {
        return systemMessage;
    }
}
